package com.ship.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.common.core.utils.SpringUtil;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.contract.bean.*;
import com.ship.contract.mapper.*;
import com.ship.contract.service.ConExcellentCasesService;
import com.ship.contract.service.ConResourceMemorandumService;
import com.ship.contract.service.ContractBaseService;
import com.ship.contract.utils.ModuleConst;
import com.ship.contract.utils.ObjectUtils;
import com.ship.system.api.BpmInstanceService;
import com.ship.system.api.model.ProcessInstanceParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tangshixuan
 * @date 2023-9-1
 */
@Service
public class ConExcellentCasesServiceImpl extends BaseServiceImpl<ConExcellentCasesMapper, ConExcellentCases> implements ConExcellentCasesService {
    @Autowired
    private ConExcellentCasesMapper conExcellentCasesMapper;

    @Autowired
    private ConResourceMemorandumMapper conResourceMemorandumMapper;

    @Autowired
    private ConResourceMainMapper conResourceMainMapper;

    @Autowired
    private ConShippingMapper conShippingMapper;

    @Autowired
    private ConShippingAgencyMapper conShippingAgencyMapper;

    @Autowired
    private ConThirdPartyMeasurementMapper conThirdPartyMeasurementMapper;

    @Autowired
    private ConInsuranceGoodsMapper conInsuranceGoodsMapper;

    @Autowired
    private ConInsurancePeopleMapper conInsurancePeopleMapper;

    @Autowired
    private ConRefuelingMapper conRefuelingMapper;

    @Autowired
    private ConLawyerMapper conLawyerMapper;

    @Autowired
    private SpringUtil springUtil;

    @Resource
    BpmInstanceService bpmInstanceService;

    @Override
    public JsonResult saveCon(ConExcellentCases conExcellentCases) {
        Long conExcellentCasesNum = this.getBaseMapper().selectCount(new LambdaQueryWrapper<ConExcellentCases>()
                .eq(ConExcellentCases::getConId,conExcellentCases.getContractName())
                .eq(ConExcellentCases::getStatus,1)
        );
        if(conExcellentCasesNum > 0){
            return JsonResult.failed("请勿重复添加");
        }
        String id = conExcellentCases.getContractName();
        String contractName = ((ContractBaseService)springUtil.getBean(ModuleConst.getContractBaseBean(conExcellentCases.getTypeOfContract()))).getContractNameById(Long.valueOf(id));
        String contractNumber = ((ContractBaseService)springUtil.getBean(ModuleConst.getContractBaseBean(conExcellentCases.getTypeOfContract()))).getContractNumberById(Long.valueOf(id));
        conExcellentCases.setContractNumber(contractNumber);
        conExcellentCases.setContractName(contractName);
        conExcellentCases.setConId(Long.valueOf(id));
        System.out.println(conExcellentCases);
        conExcellentCases.setCreateBy(SecurityUtils.getUsername());
        conExcellentCases.setCreateDate(new Date());
        conExcellentCases.setStatus(0);
        conExcellentCases.setApplicant(SecurityUtils.getUsername());
        conExcellentCases.setArchiveTime(new Date());
        super.save(conExcellentCases);

        if(conExcellentCases.getTypeOfContract() != null && conExcellentCases.getConId() != null){
            //将对应的合同状态置为优秀案例
            if(conExcellentCases.getTypeOfContract() == 1){
                ConResourceMain contract = conResourceMainMapper.getById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","资源合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",1);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conResourceMainMapper.updateById(contract);
            }else if(conExcellentCases.getTypeOfContract() == 2){
                ConShipping contract = conShippingMapper.getById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","船运合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",2);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conShippingMapper.updateById(contract);
            }else if(conExcellentCases.getTypeOfContract() == 3){
                ConShippingAgency contract = conShippingAgencyMapper.selectById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","船舶代理合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",3);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conShippingAgencyMapper.updateById(contract);
            }else if(conExcellentCases.getTypeOfContract() == 4){
                ConThirdPartyMeasurement contract = conThirdPartyMeasurementMapper.selectById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","第三方计量合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",4);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conThirdPartyMeasurementMapper.updateById(contract);
            }else if(conExcellentCases.getTypeOfContract() == 5){
                ConInsuranceGoods contract = conInsuranceGoodsMapper.selectById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","保险合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",5);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conInsuranceGoodsMapper.updateById(contract);
            }else if(conExcellentCases.getTypeOfContract() == 6){
                ConRefueling contract = conRefuelingMapper.selectById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","加油合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",6);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conRefuelingMapper.updateById(contract);
            }else if(conExcellentCases.getTypeOfContract() == 7){
                ConLawyer contract = conLawyerMapper.selectById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","律师合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",7);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conLawyerMapper.updateById(contract);
            }else if(conExcellentCases.getTypeOfContract() == 8){
                ConResourceMemorandum contract = conResourceMemorandumMapper.getById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","资源合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",8);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conResourceMemorandumMapper.updateById(contract);
            }else if(conExcellentCases.getTypeOfContract() == 9){
                ConInsurancePeople contract = conInsurancePeopleMapper.selectById(conExcellentCases.getConId());
                contract.setExcellent(1);
                ProcessInstanceParam param = new ProcessInstanceParam();
                param.setBussinessId(contract.getId());
                param.setBpmKey(BpmInstanceService.CON_RESOURCE_BOM_KEY);
                Map<String,Object> map = new HashMap<>();
                try {
                    map = ObjectUtils.objectToMap(conExcellentCases);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                map.put("moduleName","保险合同");
                map.put("reason",conExcellentCases.getExcellent());
                map.put("serviceType",9);
                param.setParams(map);
                String processInstanceId = bpmInstanceService.startProcessInstance(param);
                conExcellentCases.setProcessInstanceId(processInstanceId);
                this.updateById(conExcellentCases);
                conInsurancePeopleMapper.updateById(contract);
            }
        }
        return JsonResult.success();
    }

    @Override
    public ConExcellentCases getAllByConId(Long conId) {
        return conExcellentCasesMapper.getAllByConId(conId);
    }
}
